package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Menu;
import com.itheima.health.service.MenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author YZY
 * @version v1.0
 * @Date 2020/4/14
 * @Description 菜单展示类
 */
@RequestMapping("menu")
@RestController
@Slf4j
public class MenuController {
    @Reference
    private MenuService menuService;

    /**
     * 编辑菜单项
     * @param menu
     * @return
     */
    @RequestMapping("/edit")
    public Result edit(@RequestBody Menu menu){
        try {
            menuService.edit(menu);
            return new Result(true,"编辑菜单项成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"编辑菜单项失败");

        }
    }

    /**
     * 根据id查询菜单项
     * @param id
     * @return
     */
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            Menu menu = menuService.findById(id);
            return new Result(true,"查询成功",menu);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询失败");
        }
    }

    /**
     * 删除菜单项
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    public Result delete(Integer id){
        log.debug("delete======id:{}",id);
        try {
            menuService.deleteById(id);
            return new Result(true,"删除菜单项成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"删除菜单项失败");

        }
    }

    /**
     * 添加菜单项
     * @param menu
     * @return
     */
    @RequestMapping("/add")
    public Result add(@RequestBody Menu menu){
        try {
            menuService.add(menu);
            return new Result(true,"添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"添加失败");
        }
    }

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        try {
            PageResult pageResult = menuService.pageQuery(queryPageBean);
            return pageResult;
        } catch (Exception e) {
            e.printStackTrace();
            return new PageResult(0L,null);
        }
    }

    /**
     * 根据用户名动态展示菜单栏
     *
     * @param username
     * @return
     */
    @RequestMapping("getMenu")
    public Result getMenu(String username) {
        try {
            List<Menu> menuList = menuService.getMenu(username);
            return new Result(true, MessageConst.ACTION_SUCCESS,menuList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.ACTION_FAIL);
        }
    }

    /*
     * 查找所有的菜单数据
     *
     * */
    @RequestMapping("findAll")
    public Result findAll(){
        try{

            List<Menu> menuList=menuService.findAll();
            return new Result(true,MessageConst.ACTION_SUCCESS,menuList);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConst.ACTION_FAIL);

        }
    }
}
